package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.Record;
	import engine.logic.SceneTransfer;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.logic.tips.TipsManage;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.MsgTipsLogic;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import flashx.textLayout.container.ScrollPolicy;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *江湖助手界面 
	 * @author gaoyq
	 * 
	 */
	public class DailyWorkLogic
	{
		
		protected const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#0070dd'>","#a335ee'>","#ff8000'>","#e5cc80'>","#ffff00'>","#ff0000'>"];
		public static var _self:DailyWorkLogic;
		
		/***/
		public static var _ui:DailyWorkUI;
		
		/***/
		private  var _bind:BindManage;
		
		/***/
		private var _bindIndex:int	=	-1;
		
		/***/
		private var _bindScoreIndex:int	=	-1;
			
		/***/
		private var _bindLevelIndex:int	=	-1;
		
		/***/
		private var _bindBoxIndex:int	=	-1;
		
		/***/
		private var _model:ModelLocator;
		
		/***/
		//private var _GroupArr:Array;
		/**所得分数*/
		private var _score:int;
		
		/**领取了几个箱子*/
		private var _boxIndex:int	=	0;
		
		/**4个等级的字符串*/
		//private var stringArr:Array;
		
		/**所有表格的信息*/
		//private var _allInfoArr:Array;
		
		//private var _indexConvoy:int;
		/**是否点击领取按钮*/
		private var _isclickGetBtn:Boolean	=	false;
		
		private var _indexConvoy:int;
		public static function getInstance():DailyWorkLogic
		{
			if(_self == null)
			{
				_self = new DailyWorkLogic()
			}
			return _self;
		}
		
		public function get ui():DailyWorkUI
		{
			if( _ui == null )
			{
				_ui	=	new DailyWorkUI();
			}
			return _ui
		}
		
		public function DailyWorkLogic()
		{
		}
		
		public function Init():void
		{
			//_GroupArr	=	[];
			_model		=	ModelLocator.getInstance();
			_bind		=	BindManage.getInstance();
			ui.scrollPanel.addView(ui.viewGroup_text);	
			ui.scrollPanel.verticalScrollPolicy	=	ScrollPolicy.AUTO;
			if(_bindScoreIndex == -1)
			{
				_bindScoreIndex	=	_bind.BindProp(_model.scene.Role,"ActivityScore",this,"addScoreFun");
			}
			//addGroupInfo();
			//得分属性.
			//ui.titBtn.buttonMode	=	false;
//			ui.titBtn.mouseChildren	=	false
		}
		
		public function BeforeShow():void
		{
			ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			_bindScoreIndex	=_bind.UnbindProp(_model.scene.Role,"ActivityScore",_bindScoreIndex);
			if(_bindScoreIndex == -1)
			{
				_bindScoreIndex	=	_bind.BindProp(_model.scene.Role,"ActivityScore",this,"addScoreFun");
			}
			if(_bindIndex == -1)
			{
				_bindIndex		=	_bind.BindRecord(_model.scene,"ActivityActionRec",addInfoFun,removeFun);
			}
		
			//绑定等级
			if(_bindLevelIndex == -1)
			{
				_bindLevelIndex	=	_bind.BindProp(_model.scene.Role,"Level",this,"getLevel");
			}
			ui.boxMc.addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
			//ui.boxMc.addEventListener(MouseEvent.ROLL_OVER,onMouseOvertHandle);
			ui.getBtn.addEventListener(MouseEvent.CLICK,onGetRewardHandle);
			ui.getBtn.addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
			ui.scrollPanel.vscroll	=	0;
			ui.effect.visible	=	false;
			//PlayCDTimerLogic.getInstance().resgis(7,pkCDTime )
		}
		
		
		/**
		 * 绑定人物等级 
		 * @param val
		 * 
		 */		
		public function set getLevel(val:int):void
		{
			var stt:Record		= 	_model.scene.GetProperties("ActivityActionRec"); 
			var arr:Array		=	stt.recArr;
			resetInfo(arr);
		}
		
		/**
		 * 绑定活跃度值 
		 * @param val
		 * 
		 */		
		public function set addScoreFun(val:int):void
		{	
			_score	=	val;
//			var str:String	=	StaticIni.getIniVar("LevelConfig_"+getLevelNum(),String(getScoreNum(val)));
//			var arr:Array	=	str.split(",");
			addInfo();
			if(_bindBoxIndex == -1)
			{
				_bindBoxIndex	=	_bind.BindProp(_model.scene.Role,"DailyActivityBoxIndex",this,"BoxIndexFun");
			}
		}
			
		/**
		 * CD模块回调函数
		 * @param val CD时间
		 * @param arr 队列信息
		 */		
		public function pkCDTime(val:int,col:int):void
		{
			var mc:MovieClip;
			var txt:TextField;
			mc		=	ui.viewGroup_text.getChildAt(_indexConvoy) as MovieClip;
			txt		=	mc.getChildByName("txt_1") as TextField;
			if(val <= 0)
			{
				txt.htmlText	=	StaticIni.getSwfLangStr("DailyEventButton_1");
			}
			else
			{	
				txt.htmlText	=	PlayCDTimerLogic.getInstance().getTimerStr(val);
			}
		}
		
		/**
		 * ActivityActionRec表格数据增加或者变化
		 * @param PropertiesName
		 * @param PropertiesValue
		 * @param data
		 * 
		 */		
		private function addInfoFun(PropertiesName:String,PropertiesValue:*,data:*):void
		{
			var stt:Record		= 	_model.scene.GetProperties("ActivityActionRec"); 
			var arr:Array		=	stt.recArr;
			//等级筛选
			resetInfo(arr);
		}
		
		/**
		 * 根据等级和完成的数据变换刷新表格
		 * @param arr
		 * 
		 */		
		private function resetInfo(arr:Array):void
		{
			var level:int			=	_model.scene.roleLevel;
			//var isFamily:String		=	String(_model.scene.GetProperties("GuildName"));
			var needLevel:int;
			var tempArr:Array		=	new Array();
			var mc:MovieClip;	
			for( var i:int=	0;i < arr.length;i++)
			{
				//needLevel = int(StaticIni.getIniVar("DayActivity_"+arr[i][0],"needLevel"));
				//if( needLevel == 0 && isFamily == "null")continue;				
//				if( level >= needLevel && int(arr[i][0]) != 0)
//				{
//					tempArr.push(arr[i]);
//				}
				//if(arr[i][2]<= 0 && isFamily == "null")continue;+
				if( arr[i][0] == 0)continue;
				if(arr[i][2] == 0 && arr[i][1] == 0 )continue  ;
				tempArr.push(arr[i]);
			}
			ui.viewGroup_text.row	=	tempArr.length;
			ui.scrollPanel.addView(ui.viewGroup_text);
			
//			if(ui.scrollPanel.height > ui.scrollPanel.viewHeight)
//			{
//				ui.scrollPanel.viewHeight	= ui.scrollPanel.height-1;
//			}
//			else
//			{
//				ui.scrollPanel.viewHeight	= ui.scrollPanel.height-1;
//			}
			var arr_temp:Array =  [] ; 
			arr_temp = setArr(tempArr) ; 
			for( var j:int=	0;j < arr_temp.length;j++)
			{
				mc = ui.viewGroup_text.getItemByIndex(j) as MovieClip ; 
				if(mc == null)return;
				if(arr_temp[j][0] == 9 )
				{
					_indexConvoy	=	j;
				}
				resetSkillUpItem(mc,arr_temp[j]);
			}
		}
		private function setArr(Arr:Array):Array
		{
			var i:int = 0; 
			for(i;i<Arr.length ; i++)
			{
				var index:int = int(StaticIni.getIniVar("DayActivity_"+Arr[i][0],"Index"));
				Arr[i][3]		= index;
			}
			Arr.sort(function setArray(oneA:Array , twoB:Array):int
			{
				 return oneA[3] - twoB[3]
			}
				); 
			return Arr;
			
		}
		/**
		 * ActivityActionRec表格数据减少（基本用不到）
		 * @param PropertiesName
		 * @param PropertiesValue
		 * @param data
		 * 
		 */		
		private function removeFun(PropertiesName:String,PropertiesValue:*,data:*):void
		{
			
		}
		
		/**
		 * 对每条任务数据处理显示 
		 * @param argMC
		 * @param pList
		 * 
		 */		
		private function resetSkillUpItem(argMC:MovieClip,pList:Array = null):void
		{
			var roleLevel:int		=	_model.scene.roleLevel;
			var vip:int				=	_model.scene.Role.GetProperties("VIPLevel");
			//var isFamily:String		=	_model.scene.GetProperties("GuildName");
			var txtStr:String;	
			var str:String;	
			var num:int	;
			var isMax:int		=	int(StaticIni.getIniVar("DayActivity_"+pList[0],"MaxCount"));
			var needLevel:int	= 	int(StaticIni.getIniVar("DayActivity_"+pList[0],"needLevel"));	
			var limeStr:String	=	"";
			argMC["txt_1"].mouseEnabled		=	false;
			if(pList[0] != 0)
			{
				argMC["btn"].visible			=	true;
			}
			
			if( isMax > 0 )
			{
				num	=	pList[2];
			}
			else
			{
				num	=	pList[2]-pList[1];
			}
			if(num < 0)num =0;
			txtStr	=	StaticIni.getSwfLangStrVar(
							StaticIni.getIniVar("DayActivity_"+pList[0],"Desc"),[num]);
			argMC["txt_1"].htmlText		=	StaticIni.getSwfLangStr(
												StaticIni.getIniVar("DayActivity_"+pList[0],"buttonType"));		
			if(num == 0)
			{
				argMC["btn"].visible		=	false;
				argMC["txt_1"].visible		=	false;
				argMC["txt_0"].htmlText		= 	"<font color='#1eff00'>"+txtStr+"</font>";
			}
			else
			{
				if(roleLevel < needLevel)
				{
					limeStr		=	StaticIni.getSwfLangStrVar(
											StaticIni.getIniVar("DayActivity_"+pList[0],"Limit"),[needLevel]);
					argMC["txt_1"].htmlText 	= 	"";
				}	
				argMC["txt_0"].htmlText			=	txtStr+limeStr;
			}
		
			if(argMC["txt_1"].htmlText == "")
			{
				argMC["btn"].visible	=	false;
			}
			//b帮派特殊处理
			if(_model.scene.Role.GetProperties("GuildName") == "" && pList[0] == 1)
			{
				limeStr		=	StaticIni.getSwfLangStr(
									StaticIni.getIniVar("DayActivity_"+pList[0],"Limit"))
				argMC["btn"].visible	=	false;
				argMC["txt_0"].htmlText	=	txtStr+limeStr;
				argMC["txt_1"].htmlText	=	"";
			}
			
			//无量玉璧特殊处理
			if((_model.scene.Role.GetProperties("WLYBCurFloor") == 1 && _model.scene.Role.GetProperties("WLYBDefLevel") < 5)
				&& pList[0] == 14)
			{
				limeStr		=	StaticIni.getSwfLangStr(
										StaticIni.getIniVar("DayActivity_"+pList[0],"Limit"))
				argMC["btn"].visible	=	false;
				argMC["txt_0"].htmlText	=	txtStr+limeStr;
				argMC["txt_1"].htmlText	=	"";
			}
			
			argMC["data"]				=	String(pList[0]);
			argMC.addEventListener(MouseEvent.CLICK,onBtnClickHandle);
		}
		
		/**
		 *  初始化viewGroup_text列表
		 * 
		 */		
		private function addGroupInfo():void
		{
			var mc:MovieClip;
			var txt_0:TextField;
			var txt_1:TextField;
			//var txt_2:TextField;
			var btn:SimpleButton;
			for( var i:int = 0; i < 11; i++ )
			{
				mc		=	ui.viewGroup_text.getChildAt(i) as MovieClip;
				btn		=	mc.getChildByName("btn") as SimpleButton;
				//txt_0	=	mc.getChildByName("txt_0") as TextField;
				//txt_1	=	mc.getChildByName("txt_1") as TextField;
				//txt_2	=	mc.getChildByName("txt_2") as TextField;
				//txt_2.mouseEnabled	=	false;
				//txt_1.mouseEnabled	=	false;
				//txt_1.text			=	"";
				//txt_0.text			=	"";
				btn.visible			=	false;
				//btn.buttonMode		=	true;
				mc.addEventListener(MouseEvent.CLICK,onBtnClickHandle);
				//btn.addEventListener(MouseEvent.CLICK,onBtnClickHandle);
//				if(i == 0)
//				{
//					btn.visible			=	false;
//				}
				//mc.visible	=	false;
				//_GroupArr.push(mc);
			}
		}
		
		/**
		 * 链接按钮点击事件 
		 * @param e
		 * 
		 */		
		private function onBtnClickHandle(e:MouseEvent):void
		{
			if(e.target as SimpleButton)
			{
				var mc:MovieClip	=	e.currentTarget as MovieClip;
				var str:String		=	StaticIni.getIniVar("DayActivity_"+mc["data"],"transition");
				//SystemSender.getInstance().CustomMsg(
					//						CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST, str);
				ui.hide();
				//swatcFun(mc["data"]);	
				if(mc["data"] == 2)
				{
					TopPlayerLogic.getInstance().addbuySp();
					return
				}
				if(mc["data"] == 8)
				{
					if(WipeOutLogic.getInstance().WipeState)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("97855"));//扫荡中不能打开
						return;
					}
						
				}
				if(mc["data"] == 15 )
				{
					PetLoic.getInstance().nxTableSelect = 1 ; 
				}
				SystemSender.getInstance().CustomMsg(
					CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST, str);	
//				 向服务端发送传送请求消息
			}		
		}
		
//		private function swatcFun(index:int):void
//		{
//			switch( index )
//			{
//				case 1:
//				{
//					//FuncPowerLogic.getInstance().ui.showOrHide();;
//					DiceLogic.getInstance().ui.showOrHide();
//					break;
//				}
//				case 2:
//				{
//					//MyPropertyLogic.getInstance().ui.showOrHide();
//					TopPlayerLogic.getInstance().addbuySp();
//					break;
//				}
//				case 3:
//				{
//					//SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST,10650);
//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ENTRY_CLONE_PLAYER_ROOM)
//					break;
//				}
//					
//				case 4:
//				{
//					SystemSender.getInstance().CustomMsg(
//						CustomToServerFlag.CLIENT_CUSTOMMSG_PLAYER_GUILD_SCENE);
//					break;
//				}
//				case 5:
//				{
//					DigCimeliaLogic.getInstance().ui.showOrHide();
//					break;
//				}
//				case 6:
//				{
//					IndustryLogic.getInstance().ui.showOrHide();
//					break;
//				}
//				case 7:
//				{
//					//MyPropertyLogic.getInstance().ui.showOrHide();
//					SceneTransfer.getInstance().tranferTo(String(75041041));
//					
//					break;
//				}
//				case 8:
//				{
//					//SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST,150559);
//					var str:String		=	StaticIni.getIniVar("DayActivity_"+index,"transition");
//					SystemSender.getInstance().CustomMsg(
//						CustomToServerFlag.CLIENT_CUSTOMMSG_SCENE_TRANSFER_REQUEST, str);
//					break;
//				}
//				case 9:
//				{
//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CONVOY_OPEN_UI);
//					break;
//				}
//				case 10:
//				{
//					TourLogic.getInstance()._ui.showOrHide();
//					break;
//				}
//				case 11:
//				{
//					EmulateLogic.getInstance().ui.showOrHide();
//					break;
//				}	
//			}			
//		}
		
		
		/**
		 * 关闭tips
		 * @param e
		 * 
		 */		
		private function onMouseOutHandle(e:MouseEvent):void
		{
			TipsManage.hideTips();
			_isclickGetBtn	=	false;
		}
		
		/**
		 * 显示提示
		 * @param e
		 * 
		 */		
//		private function onMouseOvertHandle(e:MouseEvent):void
//		{
//			//TipsManage.showTips(String(1111111111));
//		}
			
		/**
		 * 领取奖励 
		 * @param e
		 * 
		 */		
		private function onGetRewardHandle(e:MouseEvent):void
		{
			_isclickGetBtn		=	true;
			ui.effect.visible	=true;
			ui.effect.gotoAndPlay(1);
			ui.effect.addFrameScript(ui.effect.totalFrames - 1,mcOverHandle);
			SystemSender.getInstance().CustomMsg(
				CustomToServerFlag.CLIENT_CUSTOMMSG_CHOOSE_ACTIVITYSCORE_BOX);	
			
//			var _tipLogic:MsgTipsLogic = MsgTipsLogic.getRes(ui.getBtn);
//			var str:String	=	StaticIni.getIniVar("LevelConfig_"+getLevelNum(),String(_boxIndex+1));
//			var Tarr:Array	=	str.split(",");
//			var color:int	=	int(StaticIni.getIniVar(String(Tarr[0]),"Color"));
//			var thingsStr:String	=	StaticIni.getSwfLangStr(Tarr[0]);
//			var strInfo:String 		= 	StaticIni.getSwfLangStrVar("UIString_1000174",
//											[link_color(thingsStr,color),String("x"+Tarr[1])] );
//			_tipLogic.showInfo(strInfo);
		}
		
		public function AfterHide():void
		{
			//_bindScoreIndex	=_bind.UnbindProp(_model.scene.Role,"ActivityScore",_bindScoreIndex);
			_bindBoxIndex	=	_bind.UnbindProp(_model.scene.Role,"DailyActivityBoxIndex",_bindBoxIndex);	
			_bindIndex		=	_bind.UnbindRecord("ActivityActionRec",_bindIndex);
			ui.boxMc.removeEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
			//ui.boxMc.removeEventListener(MouseEvent.ROLL_OVER,onMouseOvertHandle);
			ui.getBtn.removeEventListener(MouseEvent.CLICK,onGetRewardHandle);
		}
		
		/**
		 * 绑定领取第几个箱子 
		 * @param val
		 * 
		 */		 
		public function set BoxIndexFun(val:int):void
		{
			_boxIndex	=	val ;
			addInfo();	
		}
		
		/**
		 * 特效播完后执行函数 
		 * 
		 */		
		private function mcOverHandle():void
		{
			ui.effect.stop();
			ui.effect.visible	=	false;
		}
		/**
		 *活跃度数据刷新 
		 * 
		 * 33020004 紫色令牌
		 */		
		private function addInfo():void
		{
			var num:int		=	getScoreNum(_boxIndex);
			var str:String = "" ; 
			if(_boxIndex==0)
			{
				str	=	StaticIni.getIniVar("LevelConfig_"+getLevelNum(),String(_boxIndex+1));
			}else
			{
				str	=	StaticIni.getIniVar("LevelConfig_"+getLevelNum(),String(_boxIndex));
			}
			
			var Tarr:Array	=	str.split(",");
			var color:int	=	int(StaticIni.getIniVar(String(Tarr[0]),"Color"));
			var thingsStr:String	=	StaticIni.getSwfLangStr(Tarr[0]);
			if(_isclickGetBtn)
			{
				var _tipLogic:MsgTipsLogic = MsgTipsLogic.getRes(ui.getBtn);
				var strInfo:String 		= 	StaticIni.getSwfLangStrVar("UIString_1000174",
					[link_color(thingsStr,color),String("x"+Tarr[1])] );
				_tipLogic.showInfo(strInfo);
			}
			if(_boxIndex < 3)
			{
				ui.lab1.htmlText	=	StaticIni.getSwfLangStrVar("UIString_1000172",[num]);	
				ui.lab3.htmlText	=	StaticIni.getSwfLangStrVar("UIString_1000174",
											[link_color(thingsStr,color),String("x"+Tarr[1])] );
				ui.lab2.htmlText	=	StaticIni.getSwfLangStrVar("UIString_1000173",[_score+"/"+num]);
				if(_score >= num)
				{
					ui.getBtn.label		=	StaticIni.getSwfLangStr("UIStr_1001770");
					ui.getBtn.enabled	=	true;	
					ActivitesGroupLogic.getInstance().addEffect(6);
				}
				else
				{
					ui.getBtn.label		=	StaticIni.getSwfLangStr("UIStr_1001771");
					ui.getBtn.enabled	=	false;
					ActivitesGroupLogic.getInstance().removeEffect(6);
				}

				//var str1:String	=	StaticIni.getIniVar("LevelConfig_"+getLevelNum(),String(_boxIndex+1));
				//var Tarr:Array	=	str1.split(",");
				//var color:int	=	int(StaticIni.getIniVar(String(Tarr[0]),"Color"));
				//var thingsStr:String	=	StaticIni.getSwfLangStr(Tarr[0]);
			}
				//全部领取完毕后
			else
			{
				ui.lab1.visible		=	false;
				ui.lab3.visible		=	false;
				ui.getBtn.visible	=	false;
				ui.boxMc.visible	=	false;
				ActivitesGroupLogic.getInstance().removeEffect(6);
				ui.lab2.htmlText	=	StaticIni.getSwfLangStr("UIString_1000290");
			}	
		}
		
		/**
		 * 给文字润色 
		 * @param str
		 * @param color
		 * @return 
		 * 
		 */		
		private  function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
		}
		
		/**
		 * 根据人物的等级返回值 
		 * @return 
		 * 
		 */		
		private function getLevelNum():int
		{
			var num:int;
			var level:int	=	_model.scene.roleLevel;
			var lenght:int	=	int(StaticIni.getIniVar("LevlRankConfig","LevlRank"));
			
			for(var i:int = 1;i < lenght+1;i++ )
			{
				var str:String	=	StaticIni.getIniVar("LevelConfig",i.toString());
				var arr:Array	=	str.split(",");
				if(int(arr[0]) <= level && level <= int(arr[1]))
				{
					num	=	i;
					return num;
				}			
			}
			return num;
		}
		
		/**
		 * 根据 
		 * @param val
		 * @return 
		 * 
		 */		
		private function getScoreNum(val:int):int
		{
			var num:int;
			num	=	int(StaticIni.getIniVar("ScoreConfig",(val+1).toString()));
			return num
		}
			
		private function onClickCloseHandle(e:NxWindowEvent):void
		{
			ui.hide();
		}
	}
}